package com.repair.auth.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.lang.UUID;
import com.repair.api.worker.request.param.WorkerRealNameAuthParam;
import com.repair.auth.pojo.dto.UserRegisterOrLoginParam;
import com.repair.auth.pojo.dto.WorkerRegisterOrLoginParam;
import com.repair.auth.pojo.vo.LoginVO;
import com.repair.auth.pojo.vo.WorkerLoginVO;
import com.repair.auth.service.AuthService;
import com.repair.auth.service.AuthWorkerService;
import com.repair.base.result.JsonResult;
import com.repair.satoken.utils.StpKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

import static com.repair.auth.constant.AuthConstant.TOKEN_PREFIX;

/**
 * @author chunyu
 * @since 2024/9/18,17:25
 */
@Slf4j
@Api(tags = "登录注册相关接口")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private AuthWorkerService authWorkerService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("发送验证码")
    @GetMapping("/sendCaptcha")
    public JsonResult<Boolean> sendCaptcha(String telephone) {
        authService.generateAndSendCaptcha(telephone);
        return JsonResult.ok(true);
    }

    @ApiOperation("师傅发送验证码")
    @GetMapping("/sendWorkerCaptcha")
    public JsonResult<Boolean> sendWorkerCaptcha(String phone) {
        authWorkerService.generateAndSendCaptcha(phone);
        return JsonResult.ok(true);
    }

    @ApiOperation("用户注册登录")
    @PostMapping("/register")
    public JsonResult<LoginVO> loginOrRegister(@RequestBody UserRegisterOrLoginParam userRegisterOrLoginParam) {
        LoginVO loginVO = authService.loginOrRegister(userRegisterOrLoginParam);
        return JsonResult.ok(loginVO);
    }

    @ApiOperation("师傅注册登录")
    @PostMapping("/workerReg")
    public JsonResult<WorkerLoginVO> workerLoginOrRegister(@RequestBody WorkerRegisterOrLoginParam workerRegisterOrLoginParam) {
        WorkerLoginVO workerloginVO = authWorkerService.loginOrRegister(workerRegisterOrLoginParam);
        return JsonResult.ok(workerloginVO);
    }

    @ApiOperation("获取token令牌")
    @GetMapping("/getToken")
    public JsonResult<String> getToken(String type) {
        if (!StpKit.USER.isLogin() && !StpKit.WORKER.isLogin()) {
            throw new NotLoginException("未登录", "", "");
        }
        String token = UUID.randomUUID().toString(false);
        String key = TOKEN_PREFIX + type + ":" + token;
        stringRedisTemplate.opsForValue().set(key, token, 10, TimeUnit.MINUTES);
        return JsonResult.ok(key);
    }

    @ApiOperation("师傅退出登录")
    @PostMapping("/workerLogout")
    public JsonResult<Boolean> workerLogout() {
        authWorkerService.workerLogout();
        return JsonResult.ok(true);
    }

    @ApiOperation("用户退出登录")
    @PostMapping("/logout")
    public JsonResult<Boolean> Logout() {
        authService.Logout();
        return JsonResult.ok(true);
    }

    @ApiOperation("师傅实名认证")
    @PostMapping("/workerRealNameAuth")
    public JsonResult<Boolean> workerRealNameAuth(@RequestBody WorkerRealNameAuthParam workerRealNameAuthParam) {
        authWorkerService.workerRealNameAuth(workerRealNameAuthParam);
        return JsonResult.ok(true);
    }
}
